<!--
 * @Author: zl
 * @Date: 2021-02-09 15:24:23
 * @LastEditTime: 2021-11-30 18:35:12
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3-element-admin\src\views\layoutpages\system\components\usersEdit.vue
-->
<template>
    <el-dialog
        :title="title"
        append-to-body
        destroy-on-close
        :model-value="showDialog"
        @close="closeDialog()"
    >
        <!-- <span>{{ rowData }}</span> -->
        <!-- 表单 -->
        <el-form
            :model="form"
            ref="formRef"
            :rules="rules"
            label-width="80px"
            :inline="false"
        >
            <el-form-item label="店铺名称" prop="name">
                <el-input
                    v-model="name"
                    placeholder="店铺名称"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="营业执照" prop="license">
                <el-input v-model="license" placeholder="营业执照" clearable>
                </el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="userName">
                <el-input v-model="userName" placeholder="姓名" clearable />
            </el-form-item>
            <el-form-item label="手机号" prop="userPhone">
                <el-input v-model="userPhone" placeholder="手机号" clearable />
            </el-form-item>
            <el-form-item label="详细地址" prop="address">
                <el-input
                    type="textarea"
                    v-model="address"
                    placeholder="字数限制为500个字"
                    maxlength="500"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="店铺简介" prop="remark">
                <el-input
                    type="textarea"
                    v-model="remark"
                    placeholder="字数限制为500个字"
                    maxlength="500"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item label="店铺状态" prop="type">
                <el-select
                    style="width: 100%"
                    v-model="type"
                    placeholder="店铺状态"
                    clearable
                >
                    <el-option
                        v-for="item in dicList"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item
                label="审核原因"
                prop="reviewReason"
                v-if="status == 2 || status == 4"
            >
                <el-input
                    type="textarea"
                    v-model="reviewReason"
                    placeholder="字数限制为500个字"
                    maxlength="500"
                    clearable
                ></el-input>
            </el-form-item>
            <el-form-item
                label="商品状态"
                v-if="
                    auth.indexOf('ROLE_SUPER') > -1 ||
                    auth.indexOf('ROLE_ADMIN') > -1
                "
            >
                <el-radio-group v-model="status">
                    <el-radio-button :label="1">审核中</el-radio-button>
                    <el-radio-button :label="2">审核不通过</el-radio-button>
                    <el-radio-button :label="3">审核通过</el-radio-button>
                    <el-radio-button :label="4">停业整顿</el-radio-button>
                </el-radio-group>
            </el-form-item>
        </el-form>

        <template v-slot:footer>
            <span>
                <el-button @click="closeDialog()">取消</el-button>
                <el-button type="primary" @click="onSubmit()">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import {
    reactive,
    toRefs,
    ref,
    onMounted,
    defineProps,
    defineEmits,
} from "vue";
const rules = {
    name: [
        {
            required: true,
            message: "请输入店铺名称",
            trigger: "blur",
        },
    ],
    license: [
        {
            required: true,
            message: "请输入营业执照",
            trigger: "blur",
        },
    ],
    userName: [
        {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
        },
    ],
    userPhone: [
        {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
        },
    ],
    address: [
        {
            required: true,
            message: "请输入详细地址",
            trigger: "blur",
        },
    ],
    reviewReason: [
        {
            required: true,
            message: "请输入审核原因",
            trigger: "blur",
        },
    ],
};
const props = defineProps({
    showDialog: {
        type: Boolean,
        default: true,
    },
    title: {
        type: String,
        default: "添加",
    },
    rowData: {
        type: Object,
        default: null,
    },
});
const emit = defineEmits(["closeDialog"]);
const { title, rowData } = toRefs(props);
const closeDialog = () => {
    emit("closeDialog", false);
};
const formRef = ref(null);
const dicList = ref([]);
const auth = ref(sessionStorage.getItem("authorities"));
const form = reactive({
    name: "",
    userId: sessionStorage.getItem("unameid"),
    license: "",
    remark: "",
    userName: "",
    userPhone: "",
    address: "",
    type: "jsz",
    reviewReason: null,
    status: 1,
});
const {
    name,
    remark,
    license,
    userName,
    userPhone,
    address,
    type,
    reviewReason,
    status,
} = toRefs(form);

/**
 * @description: 初始化
 * @param {*}
 * @return {*}
 */
rowData.value &&
    ((name.value = rowData.value.name),
    (license.value = rowData.value.license),
    (remark.value = rowData.value.remark),
    (userName.value = rowData.value.userName),
    (userPhone.value = rowData.value.userPhone),
    (type.value = rowData.value.type),
    (status.value = rowData.value.status),
    (reviewReason.value = rowData.value.reviewReason),
    (address.value = rowData.value.address));
/**
 * @description: 获取字典项类型
 * @param {*}
 * @return {*}
 */
const getDicList = async (dicCode) => {
    const { code, data } = await VE_API.system.dictionaryEntityByCode({
        dicCode: dicCode,
    });
    if (code == "00") {
        dicList.value = data;
    }
    dicList.value = data;
};
/**
 * @description:提交
 * @param {*}
 * @return {*}
 */
const onSubmit = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            let res;
            if (title.value == "添加") {
                res = await VE_API.system.payStoreAdd(form);
            } else {
                form.userId = null;
                if (form.status == 3) {
                    form.reviewReason = "";
                }
                res = await VE_API.system.payStoreEdit({
                    id: rowData.value.id,
                    ...form,
                });
            }
            const { code } = res;
            if (code == "00") {
                closeDialog();
            }
        } else {
            console.log("error submit!!");
            return false;
        }
    });
};
onMounted(async () => {
    await getDicList("dpzt");
});
</script>

<style lang="scss" scoped></style>
